package db;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class ebDB {

    /*
     * Odmah pravimo objekat
     */
    private static ebDB instance = new ebDB();
    private final String dbUrl = "jdbc:sqlserver://";
    private final String dbServer = "192.168.16.101";
    //private final String dbServer = "localhost";
    private final String dbPort = "1433";
    private final String dbName = "RAF2008";
    private final String dbUser = "sa";
    private final String dbPass = "student";
    private Connection dbConnection = null;
    private DatabaseMetaData dbMetaData = null;

    private ebDB() {

    }

    public static ebDB getInstance() {
        return instance;
    }

    private String getConnectionUrl() {
        String tmp = "";
        tmp += dbUrl;
        tmp += dbServer;
        tmp += ":" + dbPort + ";";
        tmp += "databaseName=" + dbName + ";";
        tmp += "selectMethod=cursor";
        return tmp;
    }

    private void getDBMetaData() {
        try {
            dbMetaData = dbConnection.getMetaData();
        } catch (Exception ex) {
            dbMetaData = null;
            Logger.getLogger(DatabaseMetaData.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean Connect() {

        try {
            Driver d = (Driver) Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            dbConnection = DriverManager.getConnection(getConnectionUrl(), dbUser, dbPass);
            getDBMetaData();
            if (isConnected()) {
                System.out.println("Connection Successful!");
            }
        } catch (Exception ex) {
            dbConnection = null;
            Logger.getLogger(ebDB.class.getName()).log(Level.SEVERE, null, ex);
        }

        return (dbConnection != null);
    }

    public void Disconnect() {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(ebDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean isConnected() {
        return (dbConnection != null);
    }

    public String getDatabaseName() {
        return dbName;
    }

    public Connection getConnection() {
        return dbConnection;
    }

    public DatabaseMetaData getDatabaseMetaData() {
        return dbMetaData;
    }

    @Override
    public String toString() {
        String tmp = "";
        java.sql.ResultSet rs = null;
        try {
            if (isConnected()) {
                tmp += "Driver Information\n";
                tmp += "\tDriver Name: " + dbMetaData.getDriverName() + "\n";
                tmp += "\tDriver Version: " + dbMetaData.getDriverVersion() + "\n";
                tmp += "\nDatabase Information " + "\n";
                tmp += "\tDatabase Name: " + dbMetaData.getDatabaseProductName() + "\n";
                tmp += "\tDatabase Version: " + dbMetaData.getDatabaseProductVersion() + "\n";
                tmp += "Avalilable Catalogs " + "\n";
                rs = dbMetaData.getCatalogs();
                while (rs.next()) {
                    tmp += "\tcatalog: " + rs.getString(1) + "\n";
                }
                rs.close();
                rs = null;
                tmp += "Tables " + "\n";
                rs = dbMetaData.getTables(dbName, "dbo", "%", null);
                while (rs.next()) {
                    tmp += "\ttable: " + rs.getString(3) + "\n";
                }
                rs.close();
                rs = null;
            } else {
                System.out.println("Error: No active Connection");
            }
        } catch (Exception ex) {
            Logger.getLogger(DatabaseMetaData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tmp;
    }
}
